#pragma once
#include <fstream>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include "util.hpp"
#include <mutex>
#include "log.hpp"

namespace ns_index
{
  struct DocInfo
  {
    std::string title;
    std::string content;
    std::string url;
    uint64_t doc_id; // 较大值保护
  };
  // 倒排
  struct InvertedElem
  {
    uint64_t doc_id;
    std::string word;
    int weight; // 权重
  };

  typedef std::vector<InvertedElem> InvertedList;
  class Index
  {
    private:
      // 正排索引利用数组的下标实现文档ID
      std::vector<DocInfo> forward_index; // 正排
      // 倒排索引(一个关键字和一组InverterElem对应)//关键字倒排拉链
      std::unordered_map<std::string, InvertedList> inverter_index;

    private:
      Index() {}
      Index(const Index&) = delete;
      Index& operator = (const Index&) = delete;

      static Index* instance;
      static std::mutex mtx;       
    public:
      ~Index() {}

    public:
      static Index* GetInstance()
      {
        if(nullptr == instance)
        {  
            mtx.lock();
            if(nullptr == instance)
            {
                instance = new Index();
            }
            mtx.unlock();
         } 
         return instance;
      }
      // 根据doc_id找到文档
      DocInfo* GetForwardIndex(const uint64_t doc_id)
      {
        if(doc_id >= forward_index.size())
        {
            std::cerr << "doc_id failed" << std::endl;
            return nullptr;
        }
        return &forward_index[doc_id];
      }
      // 关键字获得倒排拉链;
      InvertedList* GetInvertdList(const std::string &word)
      {
        auto iter = inverter_index.find(word);
        if(iter == inverter_index.end())
        {
            std::cerr << word << "have no InvertedList" << std::endl;
            return nullptr;
        }

        return &(iter->second); 
      }

      // 构建索引
     // data/raw_html/raw.txt
      bool BuildIndex(const std::string &input) // 解析去标签后的文档
      {
          std::ifstream in(input, std::ios::in | std::ios::binary);
          if(!in.is_open())
          {
            std::cerr << input << "open error" << std::endl;
            return false;
          }

          std::string line;
          //parse时文件按行解析
          int count = 10;
          while(std::getline(in, line))
          {
              //正排索引
              DocInfo* doc = BuildForwardIndex(line);
              if(nullptr == doc)
              {
                  std::cerr << "bulid" << line <<" error " << std::endl;
                  continue;
              }

              BuildInvertedIndex(*doc);
              count++;
              if(0 == count % 50)
              {
                // std::cout<< "当前已经建立索引数" << count<< std::endl;
                LOG(NORMAL, "已建立索引" + std::to_string(count));
              }
          }

          return true;
      }
    private:
        DocInfo* BuildForwardIndex(const std::string &line)
        {
            std::vector<std::string> results;
            const std::string sep = "\3";
            //切分
            ns_util::StringUtil::SplitString(line, &results, sep);
            if(results.size() != 3)
            {
                return nullptr;
            }
            //填充
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_index.size();//数组特性

            forward_index.push_back(doc);
            return &forward_index.back();
        }
        bool BuildInvertedIndex(const DocInfo &doc)
        {
            //DocInfo(title content.url id)
            //word->in
            struct word_cnt{
                int title_cnt;
                int content_cnt;
                word_cnt():title_cnt(0), content_cnt(0){}
            };
            //标题词频映射
            std::unordered_map<std::string, word_cnt> word_map;
            
            std::vector<std::string> title_words;
            ns_util::JiebaUtil::CutString(doc.title, &title_words);//*out

            for(std::string &s : title_words)//要获取转换小写,
            {
                //存在获取,不存在新建
              boost::to_lower(s);
                word_map[s].title_cnt++;
            }

            //内容词频统计
            std::vector<std::string> content_words;
            ns_util::JiebaUtil::CutString(doc.content, &content_words);
            for(auto s : content_words)
            {
              boost::to_lower(s);
              word_map[s].content_cnt++;
            }

#define X 10
#define Y 1
            for(auto &word_pair :word_map)//结果保存
            {
                InvertedElem item;
                item.doc_id = doc.doc_id;
                item.word = word_pair.first;
                item.weight = X * word_pair.second.title_cnt + Y * word_pair.second.content_cnt;
                InvertedList &inverted_list = inverter_index[word_pair.first];
                inverted_list.push_back(std::move(item));
            }
            return true;
        }
  };

  Index* Index::instance = nullptr;
  std::mutex Index::mtx;
  InvertedElem item;

}
